package com.shuqing.controller;

import com.shuqing.domain.Book;
import com.shuqing.domain.User;
import com.shuqing.service.BookService;
import com.shuqing.utils.PageResult;
import com.shuqing.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @program: ShuqingLibrary
 * @ClassName BookController
 * @description:
 * @author: Ats
 * @create: 2025-03-31 15:04
 * @Version 1.0
 */
@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    private BookService bookService;


    @RequestMapping("selectNewbooks")
    public ModelAndView selectNewbooks(){
        //分页数据，首页显示第一页，每页显示5条数据
        int pageNum = 1;
        int pageSize = 5;
        //查询结果
        PageResult pageResult = bookService.selectNewBooks(pageNum, pageSize);
        //视图，为了打包数据到前端页面
        ModelAndView mv = new ModelAndView();
        //设置视图文件名
        mv.setViewName("books_new");
        //把结果数据给前端
        mv.addObject("pageResult",pageResult);
        return mv;
    }

    @ResponseBody
    @RequestMapping("/findById")
    public Result<Book> findById(Integer id){
        Book book = bookService.findBookById(id);
        return new Result<Book>(true, "查询成功", book);
    }

    @ResponseBody
    @RequestMapping("/borrowBook")
    public Result borrowBook(Book book, HttpSession session){
        User user = (User) session.getAttribute("USER_SESSION");
        book.setBorrower(user.getName());
        Integer borrowBook = bookService.borrowBook(book);
        if (borrowBook > 0){
            return new Result(true, "图书借阅成功，请到图书馆前台处领取图书！");
        }
        return new Result(false, "借阅失败，请联系图书管理员！");
    }

    @ResponseBody
    @RequestMapping("/search")
    public ModelAndView search(Book book,Integer pageNum, Integer pageSize, HttpServletRequest request) {
        //如果前端没有设置页面，那么默认第一页
        if (pageNum == null) {
            pageNum = 1;
        }
        //如果前端没有设置页面显示条数，那么默认每页显示10条数据
        if (pageSize == null) {
            pageSize = 10;
        }

        String uri = request.getRequestURI();
        PageResult pageResult = bookService.searchBooks(book, pageNum, pageSize);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("books");
        mv.addObject("pageResult", pageResult);
        //回显查询输入框的数据
        mv.addObject("search", book);
        //显示页码
        mv.addObject("pageNum", pageNum);
        //当前页面，改变页码时，路径不变
        mv.addObject("gourl",uri);
        return mv;
    }

    @RequestMapping("/addBook")
    public Result addBook(Book book){
        Integer res = bookService.addBook(book);
        if (res!=1){
            return new Result(false,"图书上架失败，请联系图书管理员！");
        }
        return new Result(true,"图书上架成功！");
    }

    @ResponseBody
    @RequestMapping("/editBook")
    public Result editBook(Book book) {
        Integer flag = bookService.editBook(book);
        if (flag > 0) {
            return new Result(true, "图书修改成功！");
        }
        return new Result(false, "图书修改失败，请联系系统管理员！");
    }

    @RequestMapping("/searchBorrowed")
    public ModelAndView searchBorrowed(Book book, HttpSession session,
                                       Integer pageNum, Integer pageSize, HttpServletRequest request) {
        //如果前端没有设置页面，那么默认第一页
        if (pageNum == null) {
            pageNum = 1;
        }
        //如果前端没有设置页面显示条数，那么默认每页显示10条数据
        if (pageSize == null) {
            pageSize = 10;
        }
        //获取当前用户
        User user = (User) session.getAttribute("USER_SESSION");
        PageResult pageResult = bookService.searchBorrowed(book, user, pageNum, pageSize);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("book_borrowed");
        mv.addObject("pageResult", pageResult);
        //回显查询输入框的数据
        mv.addObject("search", book);
        //显示页码
        mv.addObject("pageNum", pageNum);
        //当前页面，改变页码时，路径不变
        mv.addObject("gourl",request.getRequestURI());
        return mv;

    }
    @ResponseBody
    @RequestMapping("/returnBook")
    public Result returnBook(Integer id, HttpSession session) {
        User user = (User) session.getAttribute("USER_SESSION");
        boolean isReturn = bookService.returnBook(id, user);
        if (isReturn) {
            return new Result(true, "图书归还成功！");
        }
        return new Result(false, "图书归还失败，请联系图书管理员！");
    }

    @ResponseBody
    @RequestMapping("/returnConfirm")
    public Result returnConfirm(Integer id) {
        boolean flag = bookService.returnConfirm(id);
        if (flag) {
            return new Result(true, "归还确认成功！");
        }
        return new Result(false, "归还确认失败，请联系图书管理员！");
    }

}
